package com.jspx.txweb.dao.impl;

import com.jspx.boot.sign.AuditingType;
import com.jspx.sober.Criteria;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.sober.ssql.SSqlExpression;
import com.jspx.txweb.dao.ConnectAppDAO;
import com.jspx.txweb.table.ConnectApp;
import com.jspx.utils.RandomUtil;
import com.jspx.utils.StringUtil;
import com.jspx.utils.ArrayUtil;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.sober.criteria.projection.Projections;
import java.util.*;


/**
 * Created by jspx.net maker
 * User: chenYuan
 * DateTime: 2018-11-20 11:48 
 * jspx.jquestion.dao.impl.SurveyDAOImpl
 */
public class ConnectAppDAOImpl extends JdbcOperations implements ConnectAppDAO {


    public ConnectAppDAOImpl() {

    }

    public ConnectApp loadConnectApp(long appId) throws Exception
    {
        return (ConnectApp)super.load(ConnectApp.class,appId);
    }
    /**
    * 排序
    *
    * @param ids      id array
    * @param sortType 排序类型
    * @return boolean 是否成功
    */
    public boolean updateSortType(Long[] ids, int sortType)
    {
        if (null == ids) return true;
        try
        {
            for (Long mId : ids)
            {
                if (mId<1) continue;
                ConnectApp connectApp = (ConnectApp) get(ConnectApp.class, mId, false);
                if (connectApp != null)
                {
                    connectApp.setSortType(sortType);
                    super.update(connectApp, new String[]{"sortType"});
                }
            }
        } catch (Exception e)
        {
            log.error(ArrayUtil.toString(ids,StringUtil.COMMAS),e);
            return false;
        }
        return true;
    }

    /**
    * 排序时间
    *
    * @param ids id array
    * @return boolean  是否成功
    */
    public boolean updateSortDate(Long[] ids)
    {
        if (null == ids) return true;
        try
        {
            for (Long mId : ids)
            {
                if (mId<1) continue;
                ConnectApp connectApp = (ConnectApp) get(ConnectApp.class, mId, false);
                if (connectApp != null)
                {
                    connectApp.setSortDate(new Date());
                    super.update( connectApp, new String[]{"sortDate"});
                }
            }
        } catch (Exception e)
        {
            log.error(ArrayUtil.toString(ids,StringUtil.COMMAS),e);
            return false;
        }
        return true;
    }
    /**
     * @param find 查询
     * @param term 条件
     * @param uid 用户id
     * @param sort 排序方式
     * @param page 页数
     * @param count 数量
     * @return    得到列表
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List<ConnectApp> getList(String[] fields,String[] find, String term, long uid, String sort, int page, int count) throws Exception {
        Criteria criteria = createCriteria(ConnectApp.class);
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(fields)) {
            criteria = criteria.add(Expression.find(fields,find));
        }
        if (uid>0) {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        criteria = SSqlExpression.getSortOrder(criteria, sort);
        return (List<ConnectApp>) criteria.setCurrentPage(page).setTotalCount(count).list(false);
    }

    /**
     * @param find 查询
     * @param term 条件
     * @param uid  用户id
     * @return 返回数据条数
     * @throws Exception 异常
     */
    public int getCount(String[] fields,String[] find, String term, long uid) throws Exception {
        Criteria criteria = createCriteria(ConnectApp.class);
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(fields)) {
            criteria = criteria.add(Expression.find(fields,find));
        }
        if (uid>0) {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return criteria.setProjection(Projections.rowCount()).intUniqueResult();
    }

    /**
     * 审核
     *
     * @param ids          id array
     * @param auditingType 审核标识
     * @return boolean 是否成功
     */
    public boolean updateAuditing(Long[] ids, int auditingType)
    {
        if (null == ids) return true;
        try
        {
            for (Long mId : ids)
            {
                if (mId<1) continue;
                ConnectApp connectApp = (ConnectApp) get(ConnectApp.class, mId, false);
                if (connectApp != null)
                {
                    connectApp.setAuditingType(auditingType);
                    connectApp.setAuditingDate(new Date());
                    if (auditingType== AuditingType.OK)
                    {
                        //随机生成密钥
                        connectApp.setSecret(RandomUtil.getRandomMD5());
                    } else
                        connectApp.setSecret(StringUtil.empty);
                    super.update(connectApp, new String[]{"auditingType", "auditingDate","secret"});
                }
            }
        } catch (Exception e)
        {
            log.error(ArrayUtil.toString(ids,StringUtil.COMMAS),e);
            return false;
        }
        return true;
    }
}